/*
 * This code is property of CoreObjects. Use, duplication and disclosure
 * in any form without permission of copyright holder is prohibited.
 *
 */
package com.nen.session;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;

/**
 * Misc utilities for handling session, beans and cookies
 * 
 * @author amitm
 */
public final class NenUtils {

    private NenUtils() {
    }

    public static Map getBeanProperties(Object obj) {
        Map props = new HashMap();

        try {
            BeanUtils.populate(obj, props);
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }

        return props;
    }

    public static String getProperty(Object bean, String propName, String defaultValue) {
        String propValue = defaultValue;
        if (bean != null && propName != null) {
            try {
                propValue = BeanUtils.getProperty(bean, propName);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            } catch (NoSuchMethodException ex) {
                ex.printStackTrace();
            }
        }
        return propValue;
    }

    public static String getParamValue(String paramValue, String defaultValue) {
        String returnValue = defaultValue;
        if (paramValue != null) {
            String temp = paramValue.trim();
            if (temp.length() > 0) {
                returnValue = temp;
            }
        }
        return returnValue;
    }

    public static Map describe(Object bean) {
        Map map = new HashMap();
        try {
            map = BeanUtils.describe(bean);
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        } catch (NoSuchMethodException ex) {
            ex.printStackTrace();
        }

        return map;
    }

}
